package com.cskaoyan.exception;

import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author stone
 * @date 2021/11/24 11:01
 */
//@Component
public class CustomExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/levelup.jsp");
        //如果想要更精细的异常处理 → 需要你自己做异常的分发
        if (ex instanceof ArithmeticException) {
            //xxx业务
        }
        if (ex instanceof ParameterException) {
            //xxx业务
        }
        if (ex instanceof NoUniqueBeanDefinitionException) {
            //xxx业务
        }
        //如果你想要响应Json数据，也可以 → 参数里有个response → JavaEE的方式响应Json
        return modelAndView;
    }
}
